package org.dtrd.modules.evaluation.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.constant.Evaluation;
import org.dtrd.modules.evaluation.entity.bean.SleepInfo;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdSleep;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdSleepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Optional;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_ADD;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * @Author QR
 * @Date 2021/11/1 1:54 PM
 */
@Api(tags = "量表评估")
@RestController
public class SleepController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdSleepService sleepService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("添加匹兹堡睡眠质量评估")
    @PostMapping(value = {"/105/44/1054401", "/100/17/1001715"})
    public Result<?> saveSleepInfo(@RequestBody SleepInfo sleepInfo){
        // 非空校验
        if (sleepInfo.getInput() == null || sleepInfo.getInput().isEmpty()) {
            return error(STATE_PARAMS_MISSING);
        }
        // 校验量表题目数量
        boolean inValidInput = sleepInfo.getInput().size() != Evaluation.ProblemNum.SLEEP.getNum();
        if (inValidInput) {
            return error(STATE_PARAMS_ERROR);
        }
        // 校验是否有选项未填, vue 数字列表未填会默认传入 -1
        Optional<Integer> any = sleepInfo.getInput()
                .stream()
                .filter(answer -> answer == -1)
                .findAny();
        if (any.isPresent()) {
            return error(STATE_PARAMS_ERROR);
        }
        SleepInfo info = sleepService.saveSleepInfo(sleepInfo);
        String logContent = String.format("添加匹兹堡睡眠质量评估，添加用户id：%d", sleepInfo.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(info);
    }

    @ApiOperation("查询匹兹堡睡眠质量评估")
    @GetMapping("/105/44/1054402")
    public Result<?> getSleepInfoLatest(@PathParam("patientId") Integer patientId){
        if (isIntParamInvalid(patientId)) {
            return error(STATE_PARAMS_MISSING);
        }
        SleepInfo sleepInfo = sleepService.getSleepInfoLatest(patientId);
        String logContent = String.format("查询匹兹堡睡眠质量评估，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(sleepInfo);
    }

    @ApiOperation("查询匹兹堡睡眠质量评估")
    @GetMapping("/105/44/1054403")
    public Result<?> getSleepInfoList(@RequestParam("patientId") Integer patientId){
        LambdaQueryWrapper<DtrdEntEvaluationRdSleep> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdSleep.class)
                .eq(DtrdEntEvaluationRdSleep::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdSleep::getCreateTime);
        List<DtrdEntEvaluationRdSleep> list = sleepService.list(queryWrapper);
        return CollectionUtil.isNotEmpty(list) ? Result.OK(list.stream().map(sleep -> new SleepInfo().parseFromPo(sleep))) : error(STATE_NO_RECORD);
    }
}
